package com.cloud.cloudvideo.controller;

import java.util.HashMap;
import java.util.Map;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cloud.cloudvideo.entity.SysUser;
import com.cloud.cloudvideo.service.LoginService;

@Controller
public class LoginController {

	@Autowired
	private LoginService loginService;
	
	
	@GetMapping("/login")
	public String tologin() {
		return "manager/login";
	}
	
	
	
	@PostMapping("/login")
	public String login(@RequestParam("uname") String username, @RequestParam("password") String password) {
        try {
        	// 从SecurityUtils里边创建一个 subject
        	Subject subject = SecurityUtils.getSubject();
        	// 在认证提交前准备 token（令牌）
        	UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        	// 执行认证登陆
            subject.login(token);
//            SysUserRoleInfoPojo userPojo = loginService.getUserByName(subject.getPrincipal().toString());
            
        }catch(Exception e) {
        	e.printStackTrace();
        }
		return "manager/index";
	}
	
	
	@RequestMapping("/menulist")
	@ResponseBody
	public Map<String,Object> menulist(){
		Subject subject = SecurityUtils.getSubject();
		SysUser user = (SysUser) subject.getPrincipal();
		Map<String,Object> map = loginService.menulist(user.getId());
		return map;
	}
	
	
	
	@GetMapping("/logout")
	public String logout() {
		Subject subject = SecurityUtils.getSubject();
		subject.logout();
		return "redirect:/login";
	}
	
	
	
}
